package com.cg.service.impl;

import com.cg.dao.RoleMapper;
import com.cg.dao.RoleSourceMapper;
import com.cg.dao.UserRoleMapper;
import com.cg.dto.*;
import com.cg.entity.Role;
import com.cg.entity.RoleSource;
import com.cg.entity.UserRole;
import com.cg.service.RoleService;
import com.cg.service.UserTokenService;
import com.cg.vo.RoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Role)表服务实现类
 *
 * @author chengong
 * @since 2023-12-10 09:56:33
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleSourceMapper roleSourceMapper;

    @Resource
    private UserTokenService userTokenService;


    @Resource
    private UserRoleMapper userRoleMapper;



    @Override
    public PageInfo<RoleVo> queryRoleByRoleName(RoleDto roleDto) {
        PageHelper.startPage(roleDto.getPageNum(),roleDto.getPageSize());
        List<RoleVo> roleVos = roleMapper.queryRoleByRoleName(roleDto.getRoleName());
        PageInfo<RoleVo> roleVoPageInfo = new PageInfo<>(roleVos);
        return roleVoPageInfo;
    }

    @Transactional
    @Override
    public Integer createRoleSource(RoleSourceDto roleSourceDto) {
        //删除原来有的权限
        roleSourceMapper.deleteByRoleId(roleSourceDto.getRoleId());

        //保存权限
        Integer roleSource = roleSourceMapper.createRoleSource(roleSourceDto);
        //删除所有用户的key值
        userTokenService.deleteTokens();

        return roleSource;
    }

    @Override
    public Integer checkRoleCode(String roleCode) {
        return roleMapper.checkRoleCode(roleCode);
    }

    /**
     * 创建角色
     */
    @Override
    public Integer insertRole(Role role) {
        Integer count = 0;
        try {
            count = roleMapper.insertRole(role);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }


    /**
     * 角色信息修改
     * @param role
     * @return
     */
    @Override
    public Integer updateRole(Role role) {
        Integer count = 0;
        try {
            count = roleMapper.updateRole(role);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 角色信息删除
     * @param roleId
     * @return
     */
    @Override
    public Result deleteRoleByRoleId(Integer roleId) {
        List<RoleSource> roleSources = roleSourceMapper.queryRoleSourceByRoleId(roleId);
        List<UserRole> userRoles = userRoleMapper.queryAllUserRole(roleId);
        if(userRoles == null || userRoles.size() == 0){
            if(roleSources == null || roleSources.size() == 0){
                //角色信息没有绑定权限
                //可以直接删除
                Integer integer = roleMapper.deleteRoleByRoleId(roleId);
                if(integer<=0){
                    return ResultUtils.returnFail("信息删除失败，可能存在异常");
                }
                return ResultUtils.returnSuccess();
            }
            return ResultUtils.returnResult(ResultEnum.FAIL_DELETE_ROLE);
        }

        //角色有绑定权限信息，
        //需要先删除绑定的权限信息，再进行删除操作
        return ResultUtils.returnResult(ResultEnum.FAIL_DELETE_ROLE);
    }
}
